let ProdTools = {}; 

let storage = window.localStorage;
// {id : num}
let prods = JSON.parse(storage.getItem("prods") || '{}');
// 增加or 追加
ProdTools.addOrUpdate = function(p){
	// 判断是否存在
	if(prods[p.id]){ // 追加
		prods[p.id] += p.num;
	}else{
		prods[p.id] = p.num;
	}
	this.saveProds(prods);
}

// 删除
ProdTools.delete = function(id){
	delete prods[id];
	this.saveProds(prods);
}


// 获取
ProdTools.getProds = function(){
	return prods;
}

// 获取总数
ProdTools.getTotalCount = function(){
	let sum = 0;
	for(let id in prods){
		sum += prods[id];
	}
	return sum;
}

ProdTools.saveProds = function(prods){
	storage.setItem("prods" , JSON.stringify(prods));
}


export default ProdTools;
